subroutine ccsdt_sym_aaa_sg(ie, t1, t2, t22, iijka, iabij, & 
                    tmp1, tmp2, tmp3, tmp4, iabci, & 
                    icoreg, e4t, e5st, icore)
use mod_iop
use mod_orbit
use mod_size
use mod_symm
use mod_t
implicit none 
integer prec2, isd2, igpu2, totgpu2
common/precsoc/prec2, isd2, igpu2, totgpu2
real*8, intent(in) :: e4t, e5st
real*4, intent(in),device :: ie(*), t1(*), t2(*), t22(*), tmp4(*)
real*4, intent(in),device :: iijka(*), iabij(*), tmp1(*), tmp2(*), tmp3(*)
real*4, intent(in),device :: iabci(*)
real*4, intent(in),device :: icoreg(*)
real*4, intent(in) :: icore(*)
!
integer i0, i1, i2, i3, nsize
integer irrepi, numi
integer ioffvvvo(noa,8), ioffvvoo2(noa, 8), ioffvo(noa, 8)
integer ioffvooo(noa,noa,8,8), ioffvvoo(noa,noa,8,8)
!
integer irrepki, irrepij, irrepjk, irrepabc, numj, numk
integer irrepj, irrepk, i, j, k, iofftmp
integer, external :: idsymsoc, irpdso
real*8, external :: nrm2so
i0 = 1
i1 = i0 + nvvoo
i2 = i1 + nvvoo
igpu=0; igpu2=0
!
!calcioff
call idxpqr_s(ioffvvvo, noa, vrta, vrta, 0, vrta, popa, 1, 1)
call idxpqr_s(ioffvvoo2, noa, vrta, vrta, 0, popa, popa, 1, 1)
call idxpqr_s(ioffvo, noa, zorb, zorb, 1, vrta, popa, 1, 1)
!
call idxpq_rs(ioffvvoo, noa, noa, vrta, vrta, 1, popa, popa, 1, 1)
call idxpq_rs(ioffvooo, noa, noa, popa, vrta, 1, popa, popa, 1, 1)
!
!=======get t1
!
call getuhf(icore, 1, 'T1AAS___')
nsize = irpdso(1, vrta, vrta, 1)
call myicopy_g(icore, t1, nsize) 
!
!=======get t2
!
call getuhf(icore(i0), 1, 'T2AAS___')
call iexpso(icore(i0), icore(i1), vrta, vrta, 0, 0, popa, popa, 0, 1, 1)
call sstgenso(icore(i1), icore(i0), nsize, nvvaa, zorb,  popa, popa, & 
             icore, 1, '1324')
nsize = idsymsoc(1, vrta, vrta, 0, popa, popa, 1)
call myicopy_g(icore(i0), t22, nsize) 
call iexpso(icore(i1), icore(i0), vrta, vrta, 0, 1, popa, popa, 1, 0, 1)
nsize = idsymsoc(1, vrta, vrta, 1, popa, popa, 1)
call myicopy_g(icore(i0), t2, nsize) 
!call sstgenso(icore(i1), t22, nsize, nvvaa, zorb,  popa, popa, & 
!             icore, 1, '1324')
!call iexpso(icore(i1), t2, vrta, vrta, 0, 1, popa, popa, 1, 0, 1)
if(ldebug) then 
igpu=1
nsize = idsymsoc(1, vrta, vrta, 1, popa, popa, 1)
write(6,*)'norm of t2', nrm2so(nsize, t2, 1)
nsize = idsymsoc(1, vrta, vrta, 0, popa, popa, 1)
write(6,*)'norm of t22', nrm2so(nsize, t22, 1)
igpu=0
endif
!
!=======get iabij(a<b,i<j) and transfer to iabij(a<b, i,j) 
!
call getuhf(icore(i0), 1, 'ABIJAAS_')
call iexpso(icore(i0), icore(i1), vrta, vrta, 0, 0, popa, popa, 0, 1, 1)
call iexpso(icore(i1), icore(i2), vrta, vrta, 0, 1, popa, popa, 1, 0, 1)
nsize = idsymsoc(1, vrta, vrta, 1, popa, popa, 1) 
call myicopy_g(icore(i2), iabij, nsize)
!call iexpso(icore(i1), iabij,     vrta, vrta, 0, 1, popa, popa, 1, 0, 1)
!
!=======get iijka(i<j,k,a) and transfer to iijka(k,a,i,j)
!
call getuhf(icore(i0), 1, 'IJKAAAS_')
call itranspso(icore(i0), icore(i1), popa, popa, 0, popa, vrta, 1, 1)
call iexpso(icore(i1), icore(i2), popa, vrta, 1, 0, popa, popa, 0, 1, 1)
nsize = idsymsoc(1, vrta, popa, 1, popa, popa, 1) 
call myicopy_g(icore(i2), iijka, nsize)
!call iexpso(icore(i1), iijka, popa, vrta, 1, 0, popa, popa, 0, 1, 1)
!
if(ldebug) then 
igpu=1
nsize = idsymsoc(1, popa, popa, 1, vrta, popa, 1) 
write(6,*)'norm of iijkaaa', nrm2so(nsize, iijka, 1)
igpu=0
endif
! 
!=======get iabci aa
!
!call getuhf(iabci, 1, 'ABCIAA__')
!if(ldebug) then 
!nsize = idsymsoc(1, vrta, vrta, 0, vrta, popa, 1) 
!write(6,*)'norm of iabciaa', nrm2so(nsize, iabci, 1)
!endif
do irrepi = 1, nirrep
   numi = popa(irrepi)
   do i = 1, numi
   iofftmp = ioffvvvo(i, irrepi)
   call getpqrso_s(icore, i, irrepi, 1, 'ABCIAAS_')
   nsize = idsymsoc(irrepi, vrta, vrta, 0, vrta, zorb, 1)
   call myicopy_g(icore, iabci(iofftmp), nsize)
!   call getpqrso(iabci(iofftmp), i, irrepi, 1, 'ABCIAA__')
   enddo
enddo
if(ldebug) then 
igpu=1
nsize = idsymsoc(1, vrta, vrta, 0, vrta, popa, 1) 
write(6,*)'norm of iabciaa', nrm2so(nsize, iabci, 1)
igpu=0
endif
!
igpu=1; igpu2=1
do irrepk = 1, nirrep 
do irrepj = 1, nirrep
do irrepi = 1, nirrep 
   irrepij = dirprd(irrepi, irrepj) 
   irrepjk = dirprd(irrepj, irrepk) 
   irrepki = dirprd(irrepk, irrepi) 
   irrepabc = dirprd(irrepij, irrepk)
   numi = popa(irrepi)
   numj = popa(irrepj) 
   numk = popa(irrepk) 
    if(irrepi>irrepj.or.irrepj>irrepk.or.irrepi>irrepk) cycle
    if(irrepi==irrepj.and.irrepj==irrepk) then
      do k = 3, numk 
      do j = 2, k-1 
      do i = 1, j-1 
      call ccsdt_calcaaa_2(ie, &
           t1(ioffvo(i,irrepi)), t1(ioffvo(j,irrepj)), t1(ioffvo(k,irrepk)), &
           t2(ioffvvoo(i,j,irrepi,irrepj)), t2(ioffvvoo(j,k,irrepj,irrepk)), &
           t2(ioffvvoo(k,i,irrepk,irrepi)), &
           t22(ioffvvoo2(i,irrepi)), t22(ioffvvoo2(j,irrepj)), &
           t22(ioffvvoo2(k,irrepk)), & 
           iabci(ioffvvvo(i,irrepi)), iabci(ioffvvvo(j,irrepj)), &
           iabci(ioffvvvo(k,irrepk)), &
           iijka(ioffvooo(i,j,irrepi,irrepj)),iijka(ioffvooo(j,k,irrepj,irrepk)),&
           iijka(ioffvooo(k,i,irrepk,irrepi)), &  
           iabij(ioffvvoo(i,j,irrepi,irrepj)),iabij(ioffvvoo(j,k,irrepj,irrepk)),& 
           iabij(ioffvvoo(k,i,irrepk,irrepi)), &
           tmp1, tmp2, tmp3, tmp4, icore,& 
           irrepi, irrepj, irrepk, i, j, k,& 
           ioffvvvo, ioffvvoo2, ioffvvoo, ioffvooo, ioffvo, &
           e4t, e5st, vrta, vrtb, popa, popb, 1)
      enddo
      enddo
      enddo
      elseif(irrepi==irrepj.and.irrepj<irrepk) then
        do k = 1, numk
        do j = 2, numj
        do i = 1, j-1
      call ccsdt_calcaaa_2(ie, &
           t1(ioffvo(i,irrepi)), t1(ioffvo(j,irrepj)), t1(ioffvo(k,irrepk)), &
           t2(ioffvvoo(i,j,irrepi,irrepj)), t2(ioffvvoo(j,k,irrepj,irrepk)), &
           t2(ioffvvoo(k,i,irrepk,irrepi)), &
           t22(ioffvvoo2(i,irrepi)), t22(ioffvvoo2(j,irrepj)), &
           t22(ioffvvoo2(k,irrepk)), & 
           iabci(ioffvvvo(i,irrepi)), iabci(ioffvvvo(j,irrepj)), &
           iabci(ioffvvvo(k,irrepk)), &
           iijka(ioffvooo(i,j,irrepi,irrepj)),iijka(ioffvooo(j,k,irrepj,irrepk)),&
           iijka(ioffvooo(k,i,irrepk,irrepi)), &  
           iabij(ioffvvoo(i,j,irrepi,irrepj)),iabij(ioffvvoo(j,k,irrepj,irrepk)),& 
           iabij(ioffvvoo(k,i,irrepk,irrepi)), &
           tmp1, tmp2, tmp3, tmp4, icore,& 
           irrepi, irrepj, irrepk, i, j, k,& 
           ioffvvvo, ioffvvoo2, ioffvvoo, ioffvooo, ioffvo, &
           e4t, e5st, vrta, vrtb, popa, popb, 1)
        enddo
        enddo
        enddo
      elseif(irrepi<irrepj.and.irrepj==irrepk) then
        do k = 2, numk
        do j = 1, k-1
        do i = 1, numi
      call ccsdt_calcaaa_2(ie, &
           t1(ioffvo(i,irrepi)), t1(ioffvo(j,irrepj)), t1(ioffvo(k,irrepk)), &
           t2(ioffvvoo(i,j,irrepi,irrepj)), t2(ioffvvoo(j,k,irrepj,irrepk)), &
           t2(ioffvvoo(k,i,irrepk,irrepi)), &
           t22(ioffvvoo2(i,irrepi)), t22(ioffvvoo2(j,irrepj)), &
           t22(ioffvvoo2(k,irrepk)), & 
           iabci(ioffvvvo(i,irrepi)), iabci(ioffvvvo(j,irrepj)), &
           iabci(ioffvvvo(k,irrepk)), &
           iijka(ioffvooo(i,j,irrepi,irrepj)),iijka(ioffvooo(j,k,irrepj,irrepk)),&
           iijka(ioffvooo(k,i,irrepk,irrepi)), &  
           iabij(ioffvvoo(i,j,irrepi,irrepj)),iabij(ioffvvoo(j,k,irrepj,irrepk)),& 
           iabij(ioffvvoo(k,i,irrepk,irrepi)), &
           tmp1, tmp2, tmp3, tmp4, icore,& 
           irrepi, irrepj, irrepk, i, j, k,& 
           ioffvvvo, ioffvvoo2, ioffvvoo, ioffvooo, ioffvo, &
           e4t, e5st, vrta, vrtb, popa, popb, 1)
        enddo
        enddo
        enddo
      elseif(irrepi<irrepj.and.irrepj<irrepk) then
        do k = 1, numk
        do j = 1, numj
        do i = 1, numi
      call ccsdt_calcaaa_2(ie, &
           t1(ioffvo(i,irrepi)), t1(ioffvo(j,irrepj)), t1(ioffvo(k,irrepk)), &
           t2(ioffvvoo(i,j,irrepi,irrepj)), t2(ioffvvoo(j,k,irrepj,irrepk)), &
           t2(ioffvvoo(k,i,irrepk,irrepi)), &
           t22(ioffvvoo2(i,irrepi)), t22(ioffvvoo2(j,irrepj)), &
           t22(ioffvvoo2(k,irrepk)), & 
           iabci(ioffvvvo(i,irrepi)), iabci(ioffvvvo(j,irrepj)), &
           iabci(ioffvvvo(k,irrepk)), &
           iijka(ioffvooo(i,j,irrepi,irrepj)),iijka(ioffvooo(j,k,irrepj,irrepk)),&
           iijka(ioffvooo(k,i,irrepk,irrepi)), &  
           iabij(ioffvvoo(i,j,irrepi,irrepj)),iabij(ioffvvoo(j,k,irrepj,irrepk)),& 
           iabij(ioffvvoo(k,i,irrepk,irrepi)), &
           tmp1, tmp2, tmp3, tmp4, icore,& 
           irrepi, irrepj, irrepk, i, j, k,& 
           ioffvvvo, ioffvvoo2, ioffvvoo, ioffvooo, ioffvo, &
           e4t, e5st, vrta, vrtb, popa, popb, 1)
        enddo
        enddo
        enddo
      endif
enddo
enddo
enddo
write(6,*)'e4t and e5st for aaa', e4t, e5st 
igpu=0; igpu2=0
return
end
